<template>
  <div class="home">
    <!--<div>-->
      <!--<img alt="头像" :src="$store.state.userInfo.portrait" class="ico">-->
      <!--<span>{{$store.state.userInfo.name}}</span>-->
    <!--</div>-->
    <Header></Header>
    <van-tabs v-model="active" class="movie_menu" @click="listMovies">
      <van-tab >
        <div slot="title">
          <span style="display: inline-block">{{getLocation}}</span><van-icon name="arrow-down" />
        </div>
        <div class="city">
          <div class="hotCity clearfix">
            <h2>热门城市</h2>
            <ul>
              <li @click="getName('北京',1)">北京</li>
              <li @click="getName('上海',10)">上海</li>
              <li @click="getName('广州',20)">广州</li>
              <li @click="getName('深圳',30)">深圳</li>
              <li @click="getName('杭州',50)">杭州</li>
              <li @click="getName('南京',55)">南京</li>
              <li @click="getName('成都',59)">成都</li>
              <li @click="getName('武汉',57)">武汉</li>
              <li @click="getName('长沙',70)">长沙</li>
            </ul>
          </div>
          <van-index-bar @select="bbb" >
            <div  class="wrapper" ref="wrapper">
                <div v-for="(item,key) in cityLocationList" sticky="false">
                <van-index-anchor :index="key"/>
                <van-cell :title="nm.nm" v-for="nm in item" @click="getName(nm.nm,nm.id)" />
              </div>
            </div>
          </van-index-bar>
        </div>
      </van-tab>
      <van-tab title="正在热映"  class="wrapper" ref="wrapper">
          <ul class="HotMovie">
            <li  v-for="item in hotMovies">
              <van-row type="flex" justify="center">
                <van-col span="5">
                  <!--电影主图-->
                  <div class="movie_img" @click="movieDetail(item.id)">
                    <img :src="item.img" alt="">
                  </div>
                </van-col>
                <van-col span="14">
                  <!--电影简介-->
                  <div class="movie_name">
                    <!--电影名-->
                    <h2>
                      <span class="name" @click="movieDetail(item.id)">{{item.nm}}</span>
                      <img :src="item.dataimg" v-if="item.version" alt="" class="dataimg">
                    </h2>
                    <!--电影评分-->
                    <p class="movie_score">
                      <span>评分</span>
                      <span>{{item.sc}}</span>
                    </p>
                    <!--主演-->
                    <p>主演：{{item.star}}</p>
                    <!--影院信息-->
                    <p>{{item.showInfo}}</p>
                  </div>
                </van-col>
                <van-col span="4">
                  <!--购票-->
                  <div class="movie_buy">
                    <span v-if="item.showst == 3" @click="buyTickets">购票</span>
                    <span v-if="item.showst == 4" @click="buyTickets"  class="advance_ale">预售</span>
                  </div>
                </van-col>
              </van-row>
            </li>
          </ul>
      </van-tab>
      <van-tab title="即将上映" class="wrapper" ref="wrapper">
        <ul  class="HotMovie">
          <li v-for="item in soonMovies" >
            <van-row type="flex" justify="center">
              <van-col span="5">
                <!--电影主图-->
                <div class="movie_img" @click="movieDetail(item.id)">
                  <img :src="item.img" alt="">
                </div>
              </van-col>
              <van-col span="14">
                <!--电影简介-->
                <div class="movie_name">
                  <!--电影名-->
                  <h2>
                    <span class="name" @click="movieDetail(item.id)">{{item.nm}}</span>
                    <img :src="item.dataimg" v-if="item.version" alt="" class="dataimg">
                  </h2>
                  <!--电影评分-->
                  <p class="movie_score">
                    <span>评分</span>
                    <span>{{item.sc}}</span>
                  </p>
                  <!--主演-->
                  <p>主演：{{item.star}}</p>
                  <!--影院信息-->
                  <p>{{item.showInfo}}</p>
                </div>
              </van-col>
              <van-col span="4">
                <!--购票-->
                <div class="movie_buy">
                  <span v-if="item.showst == 3" @click="buyTickets">购票</span>
                  <span v-if="item.showst == 4" class="advance_ale" @click="buyTickets">预售</span>
                </div>
              </van-col>
            </van-row>
          </li>
        </ul>
      </van-tab>
      <van-tab>
        <div slot="title">
          <van-icon name="search" />
        </div>
        <!-- 展示搜索内容 -->
        <div class="search_body">
            <div class="search_input">
              <div class="search_input_wrapper">
                <!-- <van-icon name="search" />
                <input type="text" @keyup="searchResult(searchValue,$event)" v-model="searchValue" placeholder="搜电影、电视剧"> -->
                <!-- <van-search placeholder="请输入搜索关键词" @keyup="searchResult(searchValue,$event)" v-model="searchValue"/> -->
                <form action="/">
                  <van-search
                    v-model="searchValue"
                    placeholder="请输入搜索关键词"
                    show-action
                    @search="searchResult(searchValue,$event)"
                  />
                </form>
              </div>
            </div>
            <div class="search_result wrapper" ref="wrapper">
                <h3>电影/电视剧/综艺</h3>
                <ul class="search_ul">
                  <li v-for="(item,index) in searchList" :key='index'>
                      <!-- 电影主图 -->
                      <img :src="item.img" alt="" @click="movieDetail(item.id)">
                      <!-- 电影描述 -->
                      <div class="search_info">
                          <div class="name-score">
                              <p>
                                <span class="one-line">{{item.nm}}</span>
                                <!-- 2d 3d 图标判断 -->
                                <span v-if="item.version == 'v3d imax'" class="imax3d"></span>
                                <span v-if="item.version == 'v2d imax'" class="imax2d"></span>
                              </p>
                              <span class="score">
                                <span class="num">{{item.sc}}</span>
                                分
                              </span>
                          </div>
                      </div>
                      <div class="detail-section">
                          <!-- 电影上映日期等 -->
                          <div class="detail-items">
                              <p>{{item.enm}}</p>
                              <p>{{item.cat}}</p>
                              <p>{{item.pubDesc}}</p>
                          </div>
                          <!-- 是否能购票 -->
                          <!-- showst=2是已经上映的  showst=3 正在上映  showst=1 未上映  showst == 4  预售-->
                          <div class="buy-btn" v-if="item.showst == 3" @click="buyTickets">购票</div>
                          <div class="buy-btn advance_ale" v-if="item.showst == 4" @click="buyTickets">预售</div>
                      </div>
                  </li>
                </ul>
            </div>
        </div>
      </van-tab>
    </van-tabs>
    <!-- <Loading v-if="isLoging" marginTop="-30%"></Loading> -->
    <Tabbar></Tabbar>
  </div>

</template>

<script>
// @ is an alias to /src
import Header from '@/components/Header.vue'
import Tabbar from '@/components/Tabbar.vue'
import Loading from '@/components/Loading.vue'
import { Dialog } from 'vant';
import { async } from 'q';
// import BScroll from 'better-scroll'
export default {
  name: 'home',
  data(){
    return{
      isLoging: false,//默认加载loading隐藏
      active: 1,//默认显示正在热映
      hotMovies:[],//正在热映数据
      soonMovies:[],//即将上映数据
      getLocation:'',//获取当前城市
      id:null,//当前城市对应id
      cityLocationList:{},//城市数据
      Letter:['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'],//定位所有城市字母
      names:{ //当前城市name以及id
        cityName:null,
        id:null
      },
      detailMovie:{
        nm:null,//电影名
        enm:null,//电影英文名
        img:null,//电影大图
        cat:null,//电影类型
        dra:null,//剧情描述
        sc:null,//电影评分
        src:null,//国家
        episodeDur:null,//电影时长
        episodeDur:null,//语言
        pubDesc:null,//上映时间
        photos:[],//电影详情图
        wish:null //想看
      },
      searchValue:'',//搜索框value值  用于绑定v-model
      searchList:[],//搜索结果展示
      msg:null,//切换到当前城市提示
      buyTicketsMessages:[ //购票提示
        '不支持购票','没得购票接口做不了','说了做不了你还点','再点你就是个沙雕','沙雕别点了','最后一句了沙雕','你还真是一个执着的沙雕'
      ],
      buyTicketsIndex:0,
    }
  },
  components: {
    Loading,
    Tabbar,
    Header,

  },
  created(){
    this.hotMovie();
    this.soonMovie();
    this.cityMovie();
    //刷新页面位置不变
    this.active = JSON.parse(sessionStorage.getItem('thisActive'))?JSON.parse(sessionStorage.getItem('thisActive')):this.active;
    //获取城市名字
    this.getLocation = JSON.parse(sessionStorage.getItem('names')).cityName?JSON.parse(sessionStorage.getItem('names')).cityName:this.getLocation;
  },
  mounted(){
    //加载Bscroll滑动块
    // this.$nextTick(() => {
    //   this.scroll = new Bscroll(this.$refs.wrapper, {})
    // })
  },
  methods:{
    getData() {
      const that = this;
      that.axios.get('/number/news').then(res =>{
      }).catch(function (err) {
        console.log(err);
      })
    },
    about(){
      this.$router.push('/about');
    },
    //获取正在热映数据
    hotMovie(){
      const  that = this;
      let  params = {}
      if(JSON.parse(sessionStorage.getItem('names'))){//点击换地址时 获取 session 存的地址
        params = {cityId:JSON.parse(sessionStorage.getItem('names'))['id']};
      }else{//第一次启动项目时
        params = {cityId:that.id}
      }
      that.axios.get('/hai/movieOnInfoList',{params:params}).then(res => {
        res.data.data['movieList'].forEach(function (item) {
          item.img = item.img.replace('w.h','128.180');
          item.dataimg = ''
          that.hotMovies.push(item);
        })
        //加载Bscroll滑动块
        // this.$nextTick(() => {
        //     this.scroll = new Bscroll(this.$refs.wrapper, {})
        // })
        //
      }).catch(function (err) {
        console.log(err);
      })
    },
    //获取即将上映数据
    soonMovie(){
      const  that = this;
      let  params = {}
      if(JSON.parse(sessionStorage.getItem('names'))){
        params = {cityId:JSON.parse(sessionStorage.getItem('names'))['id']};
      }else{
        params = {cityId:that.id}
      }
      that.soonMovies = [];
      that.axios.get('/hai/movieComingList',{params:params}).then(res => {
        res.data.data['comingList'].forEach(function (item) {
          item.img = item.img.replace('w.h','128.180');
          item.dataimg = ''
          that.soonMovies.push(item);
        })
        //加载Bscroll滑动块
        // this.$nextTick(() => {
        //     this.scroll = new Bscroll(this.$refs.wrapper, {})
        // })
      }).catch(function (err) {
        console.log(err);
      })
    },
    //导航切换事件
    listMovies(index){
      sessionStorage.thisActive = index;
    },
    //获取定位数据
    cityMovie(){
      const  that = this;
      //获取当前城市
      this.axios.get('/hai/getLocation').then(res =>{ //写的有点问题，不应该用请求，登录时已经请求了
        that.id = res.data.data.id;
        if(JSON.parse(sessionStorage.getItem('names'))['cityName']){
          that.getLocation = JSON.parse(sessionStorage.getItem('names'))['cityName']
        }else{
          that.getLocation = res.data.data.nm;
        }
        //当选择的城市不是当地城市时提示切换到当前城市
        if(that.getLocation !== res.data.data.nm){ 
          //设置当前城市
          that.msg = res.data.data.nm;
          that.msg = '切换到'+that.msg;
           Dialog.confirm({
              title: '定位',
              message: this.msg
            }).then(() =>{
              that.getLocation = res.data.data.nm;
              that.names.cityName = res.data.data.nm;
              that.names.id = res.data.data.id;
              that.$store.commit('add', that.names);
              window.location.reload();
            })  
        }
      }).catch(err =>{

      })
      //获取 所有城市
      that.axios.get('/hai/cityList').then(res =>{
        that.Letter.forEach(item =>{
          that.cityLocationList[item] = [];
        })

        res.data.data.cities.forEach(item =>{
          switch(item.py.slice(0,1)){
            case 'a':{
              that.cityLocationList.A.push(item);
              break;
            }
            case 'b':{
              that.cityLocationList.B.push(item);
              break;
            }
            case 'c':{
              that.cityLocationList.C.push(item);
              break;
            }
            case 'd':{
              that.cityLocationList.D.push(item);
              break;
            }
            case 'e':{
              that.cityLocationList.E.push(item);
              break;
            }
            case 'f':{
              that.cityLocationList.F.push(item);
              break;
            }
            case 'g':{
              that.cityLocationList.G.push(item);
              break;
            }
            case 'h':{
              that.cityLocationList.H.push(item);
              break;
            }
            case 'i':{
              that.cityLocationList.I.push(item);
              break;
            }
            case 'j':{
              that.cityLocationList.J.push(item);
              break;
            }
            case 'k':{
              that.cityLocationList.K.push(item);
              break;
            }
            case 'l':{
              that.cityLocationList.L.push(item);
              break;
            }
            case 'm':{
              that.cityLocationList.M.push(item);
              break;
            }
            case 'n':{
              that.cityLocationList.N.push(item);
              break;
            }case 'o':{
            that.cityLocationList.O.push(item);
            break;
            }
            case 'p':{
              that.cityLocationList.P.push(item);
              break;
            }
            case 'q':{
              that.cityLocationList.Q.push(item);
              break;
            }
            case 'r':{
              that.cityLocationList.R.push(item);
              break;
            }
            case 's':{
              that.cityLocationList.S.push(item);
              break;
            }
            case 't':{
              that.cityLocationList.T.push(item);
              break;
            }
            case 'u':{
              that.cityLocationList.U.push(item);
              break;
            }
            case 'v':{
              that.cityLocationList.V.push(item);
              break;
            }
            case 'w':{
              that.cityLocationList.W.push(item);
              break;
            }
            case 'x':{
              that.cityLocationList.X.push(item);
              break;
            }
            case 'y':{
              that.cityLocationList.Y.push(item);
              break;
            }
            case 'z':{
              that.cityLocationList.Z.push(item);
              break;
            }

          }
        })
        
      }).catch(err =>{

      })
    },
    // 购票
    buyTickets(){
      Dialog.alert({
        message:this.buyTicketsMessages[this.buyTicketsIndex]
      })
      if(this.buyTicketsIndex !=this.buyTicketsMessages.length-1){
        this.buyTicketsIndex++;
      }else{
        this.buyTicketsIndex = 0;
      }
     
    },
    bbb(index){

    },
    //点击替换定位城市
    getName(name,id){
      this.isLoging = true;
      this.getLocation = name;
      this.names.cityName = name;
      this.names.id = id;
      this.$store.commit('add', this.names);
      window.location.reload();
    },
    //进入电影详情页面
    movieDetail(id){
      const params = {
        movieId : id ,
      }
      this.axios.get('/hai/detailmovie',{params:params}).then(res =>{
            res.data.data.detailMovie.img = res.data.data.detailMovie.img.replace('w.h','148.208');
            res.data.data.detailMovie.photos.forEach((item,index) =>{
              res.data.data.detailMovie.photos[index] = item.replace('w.h','148.208');
            })
            this. detailMovie = {
              nm:res.data.data.detailMovie.nm,//电影名
              enm:res.data.data.detailMovie.enm,//电影英文名
              img:res.data.data.detailMovie.img,//电影大图
              cat:res.data.data.detailMovie.cat,//电影类型
              dra:res.data.data.detailMovie.dra,//剧情描述
              sc:res.data.data.detailMovie.sc,//电影评分
              src:res.data.data.detailMovie.src,//国家
              episodeDur:res.data.data.detailMovie.episodeDur,//电影时长
              episodeDur:res.data.data.detailMovie.episodeDur,//语言
              pubDesc:res.data.data.detailMovie.pubDesc,//上映时间
              photos:res.data.data.detailMovie.photos,//电影详情图
              wish:res.data.data.detailMovie.wish,//想看
            }
            this.$store.commit('getMovieDeatil',this.detailMovie);
      })
      
      this.$router.push('/movie_detail');
    },
    async searchResult(val,ev){
      try{
        const id = JSON.parse(sessionStorage.getItem('names')).id;
        const params = { 
          cityId : id, 
          kw : Mtils.utils.makePy(val),//汉字转义成拼音进行搜索
        }
        const res = await this.axios.get('/hai/searchList',{params:params});
        if(!res.data.data.movies){
            Dialog.alert({
              message:'没得数据！'
            })
          }else{
              this.searchList = res.data.data.movies.list;
              this.searchList.forEach((res,index) =>{
                this.searchList[index].img = res.img.replace('w.h','148.208');
              })
              //加载Bscroll滑动块
              // this.$nextTick(() => {
              //     this.scroll = new Bscroll(this.$refs.wrapper, {})
              // })
          }
      }
      catch(err){
        console.log(err)
      }
    },
    // searchResult(val,ev){//获取搜索数据
    //   const id = JSON.parse(sessionStorage.getItem('names')).id;
    //   const params = { 
    //     cityId : id, 
    //     kw : Mtils.utils.makePy(val),//汉字转义成拼音进行搜索
    //   }
    //   if(val){
    //     this.axios.get('/hai/searchList',{params:params}).then(res =>{
    //       //搜不到数据时提示
    //       if(!res.data.data.movies){
    //         Dialog.alert({
    //           message:'没得数据！'
    //         })
    //       }else{
    //           this.searchList = res.data.data.movies.list;
    //           this.searchList.forEach((res,index) =>{
    //             this.searchList[index].img = res.img.replace('w.h','148.208');
    //           })
    //           //加载Bscroll滑动块
    //           // this.$nextTick(() => {
    //           //     this.scroll = new Bscroll(this.$refs.wrapper, {})
    //           // })
    //       }
    //     })
    //   }
    // },
  },

}
</script>
<style lang="less">
    .home .movie_menu .search_input{
      padding: 0 !important;
    }
    .home .movie_menu .search_input .search_input_wrapper{
      display:block !important;
      border-radius: 0 !important;
      border-left: none !important; 
      border-right: none !important; 
    }
    .advance_ale{
      background-color: #3c9fe6 !important;
    }
    .van-search .van-cell{
       padding: 0 !important;
    }
    .home .movie_menu .search_input .search_input_wrapper .van-icon-search {
      line-height: 32px !important;
    }
  //此处可以直接调用css  2d  3d图标 
  .imax2d{
    background-image: url(../images/2d.png);
    width: 43px;
    background-size: contain;
    background-repeat: no-repeat;
    height: 14px;
    margin-left: 5px;
    display: inline-block;
  }
  .imax3d{
    background-image: url(../images/3d.png);
    width: 43px;
    background-size: contain;
    background-repeat: no-repeat;
    height: 14px;
    margin-left: 5px;
    display: inline-block;
  }

  .van-index-anchor--sticky{
    top: 94px;
  }
  .van-tab--active{
    color: #f44;
  }
  .van-tabs__content{
    flex: 1;
    overflow: auto;
    margin-bottom: 50px;
  }
  .van-tab .van-icon-search{
    font-size: 20px;
    vertical-align: middle;
  }
  .city{
    background: #fff5f0;
    padding-top: 20px;
    flex: 1;
    overflow: auto;
    .hotCity{
      h2{
        padding-left: 15px;
        line-height: 30px;
        font-size: 14px;
        background: #f0f0f0;
        font-weight: 400;
        text-align: left;
      }
      ul{
       li{
         float: left;
         background: #fff;
         width: 29%;
         height: 33px;
         margin-top: 15px;
         margin-left: 3%;
         padding: 0 4px;
         border: 1px solid #e6e6e6;
         border-radius: 3px;
         line-height: 33px;
         text-align: center;
         -webkit-box-sizing: border-box;
         box-sizing: border-box;
       }
      }
    }
  }
  .van-cell{
    background-color: transparent;
    padding: 3px 15px;
  }
  .van-index-bar{
    text-align: left;
    margin-top: 20px;
  }
  .van-index-anchor{
    background: #f0f0f0;
  }
  .van-cell:not(:last-child)::after{
    border-bottom: transparent;
  }
  .van-tabs__wrap{
    position: fixed;
    top: 50px;
    z-index: 999;
  }
  .van-tab__pane{
    margin-top: 50px;
  }
  .home{
    header{
      width: 100%;
      position: relative;
      height: 50px;
      line-height: 50px;
      text-align: center;
      color: #fff;
      background: #07c160;
      z-index: 9999;
      position: fixed;
      top: 0;
      width: 100%;
      h1{
        font-size: 17px;
        font-weight: 400;
      }
    }
    .movie_menu{
      width: 100%;
      .search_input{
        padding: 8px 10px;
        .search_input_wrapper{
          padding: 0 0 0 10px;
          border: 1px solid #e6e6e6;
          border-radius: 5px;
          display: flex;
          line-height: 20px;
          input{
            border: none;
            font-size: 13px;
            color: #333;
            padding: 4px 0;
            outline: none;
            margin-left: 5px;
            width: 100%;
          }
          .van-icon-search{
            line-height: 28px;
          }
        }
      }
      .search_result{
        h3{
          height: 40px;
          line-height: 40px;
          padding-left: 10px;
        }
        .search_ul{
          padding-left: 15px;
          border-top: 1px solid #e6e6e6;
          border-bottom: 1px solid #e6e6e6;
          li{
            padding: 12px 0;
            min-height: 90px;
            border-bottom: 1px solid #e6e6e6;
            img{
                background-color: #eee;
                width: 64px;
                height: 90px;
                float: left;
                margin-right: 10px;
                position: relative;
                z-index:9;
            }
            .search_info{
                margin-right: 15px;
                margin-top: 1px;
               .name-score {
                  font-size: 16px;
                  color: #222;
                  display: -webkit-box;
                  display: flex;
                  -webkit-box-pack: justify;
                  justify-content: space-between;
                  margin-bottom: 2px;
                  p{
                    display: -webkit-box;
                    display: flex;
                    -webkit-box-align: center;
                    align-items: center;
                    -webkit-box-flex: 1;
                    flex-grow: 1;
                    max-height: 24px;
                    overflow: hidden;
                    white-space: nowrap;
                    text-overflow: ellipsis;
                    .one-line{
                      display: inline-block;
                      font-weight: 700;
                      font-size: 17px;
                      white-space: nowrap;
                      text-overflow: ellipsis;
                      overflow: hidden;
                    }
                  }
                  .score{
                    color: #fa0;
                    font-size: 10px;
                    flex-shrink: 0;
                    padding-left: 5px;
                    padding-top: 4px;
                    .num{
                      font-size: 16px;
                    }
                  }
              }
            }
            .detail-section{
              position: relative;
              .detail-items{
                -webkit-box-flex: 1;
                flex-grow: 1;
                overflow: hidden;
                padding-right: 10px;
                margin-right: 48px;
                min-height: 27px;
                p{
                  font-size: 13px;
                  color: #666;
                  margin-top: 2px;
                }
              }
              .buy-btn{
                background-color: #ef4238;
                color: #fff;
                border-radius: 4px;
                font-size: 12px;
                -webkit-align-self: flex-end;
                align-self: flex-end;
                flex-shrink: 0;
                width: 48px;
                text-align: center;
                height: 27px;
                line-height: 27px;
                right: 15px;
                position: absolute;
                top: 0;
                bottom: 0;
                margin: auto;
              }
            }
          }
          li:nth-last-child(1){
            border-bottom: none;
          }
        }
      }
      .HotMovie{
        margin: 0 12px;
        li{
          margin-top: 10px;
          align-items: center;
          padding-bottom: 10px;
          border-bottom: 1px solid #e6e6e6;
        }
        .movie_img{
          width: 65px;
          height: 90px;
          display: inline-block;
          img{
            width: 100%;
          }
        }
        .movie_name{
          width: 100%;
          margin-left: 5px;
          display: inline-block;
          position: relative;
          text-align: left;
          .name{
            font-size: 17px;
            width: 150px;
            overflow: hidden;
            display: inline-block;
            white-space: nowrap;
            text-overflow: ellipsis;
          }
          .dataimg{
            position: absolute;
            width: 50px;
            top: 0;
            right: 0;
          }
          p{
            font-size: 13px;
            color: #666;
            line-height: 22px;
            width: 200px;
            overflow: hidden;
            white-space: nowrap;
            text-overflow: ellipsis;
          }
          .movie_score{
            margin-top: 5px;
            span:nth-of-type(2){
              color: #07c160;
              margin-left: 5px;
              font-weight: 600;
            }
          }
        }
        .movie_buy{
          position: relative;
          top: 50%;
          transform: translateY(-50%);
          span{
            display: inline-block;
            width: 47px;
            height: 27px;
            line-height: 28px;
            text-align: center;
            background-color: #f44;
            color: #fff;
            border-radius: 4px;
            font-size: 12px;
            cursor: pointer;
          }
        }
      }

    }
  }
</style>
